﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using ProjectManageSystemApi.Models;
using ProjectManageSystemApi.Services;
using ProjectManageSystemApi.Utilities;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace ProjectManageSystemApi.Controllers
{
    /// <summary>
    /// 供应商物料管理Api
    /// </summary>
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = Grouping.GroupName_v1)]
    public class SupplierGoodsController : ControllerBase
    {
        readonly ILogger<SupplierGoodsController> _logger;
        readonly ISupplierGoodsService _supplierGoodsService;
        readonly IUserServices _user;
        public SupplierGoodsController(ILogger<SupplierGoodsController> logger, ISupplierGoodsService spplierGoodsService, IUserServices user)
        {
            _logger = logger;
            _supplierGoodsService = spplierGoodsService;
            _user = user;
        }

        [HttpPost, Route("GetAll")]
        public ApiResult GetAll([FromBody] PageBo<SupplierGoodsParams> pageBo)
        {
            if (pageBo == null)
            {
                pageBo = new PageBo<SupplierGoodsParams>();
            }
            if(pageBo.Param == null) 
            {
                pageBo.Param = new SupplierGoodsParams();
            }
            var data = _supplierGoodsService.GetAll(pageBo);
            return ApiResultStatus.Success(data);
        }

        [HttpPost, Route("Create")]
        public ApiResult Create([FromBody] SupplierGoodsInfoDto supplierGoodsInfo)
        {
            if (supplierGoodsInfo == null || supplierGoodsInfo.SupplierId == 0 || string.IsNullOrWhiteSpace(supplierGoodsInfo.GoodsName)) 
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }

            string token = Request.GetToken();
            if (string.IsNullOrWhiteSpace(token))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_user.GetUserIdByToken(token, out long userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            supplierGoodsInfo.Id = 0;
            if (_supplierGoodsService.ExistBySupplierIdAndGoods(supplierGoodsInfo.SupplierId, supplierGoodsInfo.GoodsName))
            {
                return ApiResultStatus.Fail(ApiResultCodes.SUPPLIER_GOODS_NAME_EXIST);
            }

            if (!_supplierGoodsService.Create(supplierGoodsInfo, userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.OPERATION_FAILED);
            }
            return ApiResultStatus.Success();
        }

        [HttpPost, Route("Update")]
        public ApiResult Update([FromBody] SupplierGoodsInfoDto supplierGoodsInfo)
        {
            if (supplierGoodsInfo == null || supplierGoodsInfo.SupplierId == 0 || supplierGoodsInfo.Id == 0 || string.IsNullOrWhiteSpace(supplierGoodsInfo.GoodsName)) 
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }

            if (!_supplierGoodsService.ExistById(supplierGoodsInfo.Id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.DATA_NOT_EXIST);
            }
            if (_supplierGoodsService.ExistBySupplierIdAndGoods(supplierGoodsInfo.SupplierId, supplierGoodsInfo.GoodsName, supplierGoodsInfo.Id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.SUPPLIER_GOODS_NAME_EXIST);
            }
            if (!_supplierGoodsService.Update(supplierGoodsInfo))
            {
                return ApiResultStatus.Fail(ApiResultCodes.OPERATION_FAILED);
            }
            return ApiResultStatus.Success();
        }

        [HttpPost, Route("Delete")]
        public ApiResult Delete(int id)
        {
            if (id == 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_supplierGoodsService.ExistById(id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.DATA_NOT_EXIST);
            }
            if (!_supplierGoodsService.Delete(id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.OPERATION_FAILED);
            }
            return ApiResultStatus.Success();
        }
    }
}
